package com.datang.pet.service.impl;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-15 08:55
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.mapper.*;
import com.datang.pet.pojo.*;
import com.datang.pet.vo.*;
import com.datang.pet.mapperExtend.TalkMapperExtend;
import com.datang.pet.mapperExtend.UserInfoMapperExtend;
import com.datang.pet.service.CommentService;
import com.datang.pet.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private TalkOneTierCommentMapper talkOneTierCommentMapper;

    @Autowired
    private TalkMapperExtend talkMapperExtend;

    @Autowired
    private UserMessageMapper userMessageMapper;

    @Autowired
    private MessageService messageService;

    @Autowired
    private TalkOneTierCommentLikeRecordMapper talkOneTierCommentLikeRecordMapper;

    @Autowired
    private TalkTwoTierCommentMapper talkTwoTierCommentMapper;

    @Autowired
    private TalkTwoTierCommentLikeRecordMapper talkTwoTierCommentLikeRecordMapper;

    @Autowired
    private UserInfoMapperExtend userInfoMapperExtend;


    /*
     * @author 顶风少年
     * @date 2019/6/15 9:37
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param addOneCommentRequest
     * @result void
     * @notify 一层评论
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void addOneComment(AddOneCommentRequest addOneCommentRequest) throws Exception {
        //添加评论
        TalkOneTierComment talkOneTierComment = new TalkOneTierComment();
        talkOneTierComment.setId(addOneCommentRequest.getId());
        talkOneTierComment.setCommentContent(addOneCommentRequest.getContent());
        talkOneTierComment.setTalkId(addOneCommentRequest.getTalkId());
        talkOneTierComment.setUserId(addOneCommentRequest.getCommentUserId());
        talkOneTierComment.setCommentDate(new Date());
        talkOneTierComment.setLikeQuantity(0);
        talkOneTierComment.setCommentQuantity(0);
        try {
            talkOneTierCommentMapper.insert(talkOneTierComment);
        } catch (Exception e) {
            throw new Exception("添加评论失败");
        }

        try {
            talkMapperExtend.increaseTalk(addOneCommentRequest.getTalkId(), "2");
        } catch (Exception e) {
            throw new Exception("喵文评论数+1失败");
        }

        //如果喵文发布人id 和 评论人id相同.则不发送消息
        if (!addOneCommentRequest.getTalkUserId().equals(addOneCommentRequest.getCommentUserId())) {
            try {
                UserMessage userMessage = new UserMessage();
                userMessage.setId(StringUtils.getId());
                userMessage.setIsRead("0");
                userMessage.setUserId(addOneCommentRequest.getTalkUserId());
                //评论内容
                userMessage.setMessage(addOneCommentRequest.getContent());
                userMessage.setMessageDate(new Date());
                //一层id
                userMessage.setModelId(addOneCommentRequest.getId());
                //评论人id
                userMessage.setMessageType("4");
                //消息关联人id
                userMessage.setExtent1(addOneCommentRequest.getCommentUserId());
                //喵文id
                userMessage.setExtent2(addOneCommentRequest.getTalkId());
                userMessageMapper.insert(userMessage);
            } catch (Exception e) {
                throw new Exception("点赞成功,增加消息失败");
            }
            //发送消息
            messageService.getMessage(addOneCommentRequest.getTalkUserId(), "4");
        }
    }

    /*
     * @author 顶风少年
     * @date 2019/6/15 11:09
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param addLikeOneCommentRequest
     * @result void
     * @notify 一级评论点赞 取消点赞
     * version 1.0
     */
    @Transactional(rollbackFor = Exception.class)
    public void addLikeOneComment(AddLikeOneCommentRequest addLikeOneCommentRequest) throws Exception {
        if (addLikeOneCommentRequest.getType().equals("1")) {
            //添加点赞记录
            TalkOneTierCommentLikeRecord talkOneTierCommentLikeRecord = new TalkOneTierCommentLikeRecord();
            talkOneTierCommentLikeRecord.setId(StringUtils.getId());
            talkOneTierCommentLikeRecord.setTalkOneTierCommentId(addLikeOneCommentRequest.getTalkOneCommentId());
            talkOneTierCommentLikeRecord.setUserId(addLikeOneCommentRequest.getLikeCommentUserId());
            talkOneTierCommentLikeRecord.setLikeDate(new Date());
            try {
                talkOneTierCommentLikeRecordMapper.insert(talkOneTierCommentLikeRecord);
            } catch (Exception e) {
                throw new Exception("增加一层点赞记录失败");
            }

            //评论点赞+1
            try {
                talkMapperExtend.increaseOneComment(addLikeOneCommentRequest.getTalkOneCommentId(), "1");
            } catch (Exception e) {
                throw new Exception("喵文评论点赞+1失败");
            }

            //一层评论作者总点赞数 -1
            try {
                userInfoMapperExtend.contrLikeQuantity(addLikeOneCommentRequest.getCommentUserId(), "1");
            } catch (Exception e) {
                throw new Exception("给一层评论作者总点赞数+1失败");
            }

            //发送消息
            //如果评论人id 和 点赞人id相同.则不发送消息
            if (!addLikeOneCommentRequest.getCommentUserId().equals(addLikeOneCommentRequest.getLikeCommentUserId())) {
                try {
                    UserMessage userMessage = new UserMessage();
                    userMessage.setId(StringUtils.getId());
                    userMessage.setIsRead("0");
                    userMessage.setUserId(addLikeOneCommentRequest.getCommentUserId());
                    userMessage.setMessage(addLikeOneCommentRequest.getContent());
                    userMessage.setMessageDate(new Date());
                    userMessage.setModelId(addLikeOneCommentRequest.getTalkId());
                    userMessage.setMessageType("5");
                    userMessage.setExtent1(addLikeOneCommentRequest.getLikeCommentUserId());
                    userMessageMapper.insert(userMessage);
                } catch (Exception e) {
                    throw new Exception("点赞成功,增加消息失败");
                }
                //发送消息
                messageService.getMessage(addLikeOneCommentRequest.getCommentUserId(), "5");
            }


        } else {
            //取消点赞
            try {
                TalkOneTierCommentLikeRecordExample talkOneTierCommentLikeRecordExample = new TalkOneTierCommentLikeRecordExample();
                TalkOneTierCommentLikeRecordExample.Criteria criteria = talkOneTierCommentLikeRecordExample.createCriteria();
                criteria.andTalkOneTierCommentIdEqualTo(addLikeOneCommentRequest.getTalkOneCommentId());
                criteria.andUserIdEqualTo(addLikeOneCommentRequest.getLikeCommentUserId());
                talkOneTierCommentLikeRecordMapper.deleteByExample(talkOneTierCommentLikeRecordExample);
            } catch (Exception e) {
                throw new Exception("删除一层点赞记录失败");
            }

            //一层评论点赞数-1
            try {
                talkMapperExtend.decrementOneComment(addLikeOneCommentRequest.getTalkOneCommentId());
            } catch (Exception e) {
                throw new Exception("一层评论喜欢数-1失败");
            }

            //一层评论作者总点赞数 -1
            try {
                userInfoMapperExtend.contrLikeQuantity(addLikeOneCommentRequest.getCommentUserId(), "2");
            } catch (Exception e) {
                throw new Exception("给一层评论作者总点赞数-1失败");
            }


        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void addTwoComment(AddTwoCommentRequest addTwoCommentRequest) throws Exception {

        //添加评论记录
        TalkTwoTierComment talkTwoTierComment = new TalkTwoTierComment();
        talkTwoTierComment.setId(addTwoCommentRequest.getId());
        talkTwoTierComment.setTalkOneTierCommentId(addTwoCommentRequest.getOneCommentId());
        talkTwoTierComment.setCommentContent(addTwoCommentRequest.getCommentContent());
        talkTwoTierComment.setUserId(addTwoCommentRequest.getTwoCommentUserId());
        talkTwoTierComment.setCommentDate(new Date());
        talkTwoTierComment.setLikeQuantity(0);
        talkTwoTierComment.setReplyUserId(addTwoCommentRequest.getReplyUserId());
        talkTwoTierComment.setTalkId(addTwoCommentRequest.getTalkId());

        try {
            talkTwoTierCommentMapper.insert(talkTwoTierComment);
        } catch (Exception e) {
            throw new Exception("添加二级评论失败");
        }

        //喵文评论次数+1
        try {
            talkMapperExtend.increaseTalk(addTwoCommentRequest.getTalkId(), "2");
        } catch (Exception e) {
            throw new Exception("喵文评论数+1失败");
        }

        //一级评论次数+1
        try {
            talkMapperExtend.increaseOneComment(addTwoCommentRequest.getOneCommentId(), "2");
        } catch (Exception e) {
            throw new Exception("一级评论次数+1失败");
        }
        //情景设定:二层评论不向原文作者发送消息.如果二层评论不是回复,则向一层评论发送消息.如果是回复,则只向二层评论人发送消息
        if (StringUtils.isNUll(addTwoCommentRequest.getReplyUserId())) {
            //如果评论人和需要回复的人是同一个则不发送消息
            if(!addTwoCommentRequest.getReplyUserId().equals(addTwoCommentRequest.getTwoCommentUserId())){
                try {
                    UserMessage userMessage = new UserMessage();
                    userMessage.setId(StringUtils.getId());
                    userMessage.setIsRead("0");
                    userMessage.setUserId(addTwoCommentRequest.getReplyUserId());
                    userMessage.setMessage(addTwoCommentRequest.getCommentContent());
                    userMessage.setMessageDate(new Date());
                    userMessage.setModelId(talkTwoTierComment.getTalkOneTierCommentId());
                    userMessage.setMessageType("7");
                    //消息关联人id
                    userMessage.setExtent1(addTwoCommentRequest.getTwoCommentUserId());
                    //喵文id
                    userMessage.setExtent2(addTwoCommentRequest.getTalkId());
                    //关联消息内容
                    userMessage.setExtent3(addTwoCommentRequest.getOhterContent());
                    userMessageMapper.insert(userMessage);
                } catch (Exception e) {
                    throw new Exception("给二层评论添加消息失败");
                }
                //发送消息
                messageService.getMessage(addTwoCommentRequest.getReplyUserId(), "7");
            }
        }else{
            //如果没有回复人.则向一级评论发送消息.但是如果二层评论人和一层评论人是同一个则不发送消息
            if (!addTwoCommentRequest.getOneCommentUserId().equals(addTwoCommentRequest.getTwoCommentUserId())) {
            try {
                UserMessage userMessage = new UserMessage();
                userMessage.setId(StringUtils.getId());
                userMessage.setIsRead("0");
                userMessage.setUserId(addTwoCommentRequest.getOneCommentUserId());
                userMessage.setMessage(addTwoCommentRequest.getCommentContent());
                userMessage.setMessageDate(new Date());
                userMessage.setModelId(talkTwoTierComment.getTalkOneTierCommentId());
                userMessage.setMessageType("6");
                //消息关联人id
                userMessage.setExtent1(addTwoCommentRequest.getTwoCommentUserId());
                //喵文id
                userMessage.setExtent2(addTwoCommentRequest.getTalkId());
                //关联消息内容
                userMessage.setExtent3(addTwoCommentRequest.getOhterContent());
                userMessageMapper.insert(userMessage);
            } catch (Exception e) {
                throw new Exception("给一层评论添加消息失败");
            }
            //发送消息
            messageService.getMessage(addTwoCommentRequest.getOneCommentUserId(), "6");
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void addLikeTwoComment(AddLikeTwoCommentRequest addLikeTwoCommentRequest) throws Exception {
        if (addLikeTwoCommentRequest.getType().equals("1")) {
            //点赞
            TalkTwoTierCommentLikeRecord talkTwoTierCommentLikeRecord = new TalkTwoTierCommentLikeRecord();
            talkTwoTierCommentLikeRecord.setId(StringUtils.getId());
            talkTwoTierCommentLikeRecord.setTalkTwoTierCommentId(addLikeTwoCommentRequest.getTwoCommentId());
            talkTwoTierCommentLikeRecord.setLikeUserId(addLikeTwoCommentRequest.getLikeUserId());
            talkTwoTierCommentLikeRecord.setLikeDate(new Date());

            try {
                talkTwoTierCommentLikeRecordMapper.insert(talkTwoTierCommentLikeRecord);
            } catch (Exception e) {
                throw new Exception("添加二层点赞记录失败");
            }

            //二级评论点赞数+1
            try {
                talkMapperExtend.increaseOrDecrementTwoComment(addLikeTwoCommentRequest.getTwoCommentId(), "1");
            } catch (Exception e) {
                throw new Exception("给二层评论点赞数+1失败");
            }

            //一层评论作者总点赞数 +1
            try {
                userInfoMapperExtend.contrLikeQuantity(addLikeTwoCommentRequest.getTwoCommentUserId(), "1");
            } catch (Exception e) {
                throw new Exception("给二层评论作者总点赞数+1失败");
            }

            //发送消息
            //如果二层评论作者 和 点赞人id相同.则不发送消息
            if (!addLikeTwoCommentRequest.getTwoCommentUserId().equals(addLikeTwoCommentRequest.getLikeUserId())) {
                try {
                    UserMessage userMessage = new UserMessage();
                    userMessage.setId(StringUtils.getId());
                    userMessage.setIsRead("0");
                    userMessage.setUserId(addLikeTwoCommentRequest.getTwoCommentUserId());
                    userMessage.setMessage(addLikeTwoCommentRequest.getContent());
                    userMessage.setMessageDate(new Date());
                    userMessage.setModelId(addLikeTwoCommentRequest.getTalkId());
                    userMessage.setMessageType("8");
                    userMessage.setExtent1(addLikeTwoCommentRequest.getLikeUserId());
                    userMessageMapper.insert(userMessage);
                } catch (Exception e) {
                    throw new Exception("点赞成功,增加消息失败");
                }
                //发送消息
                messageService.getMessage(addLikeTwoCommentRequest.getTwoCommentUserId(), "8");
            }

        } else {
            //取消点赞
            TalkTwoTierCommentLikeRecordExample talkTwoTierCommentLikeRecordExample = new TalkTwoTierCommentLikeRecordExample();
            TalkTwoTierCommentLikeRecordExample.Criteria criteria = talkTwoTierCommentLikeRecordExample.createCriteria();
            criteria.andTalkTwoTierCommentIdEqualTo(addLikeTwoCommentRequest.getTwoCommentId());
            criteria.andLikeUserIdEqualTo(addLikeTwoCommentRequest.getLikeUserId());
            try {
                talkTwoTierCommentLikeRecordMapper.deleteByExample(talkTwoTierCommentLikeRecordExample);
            } catch (Exception e) {
                throw new Exception("删除二级评论点赞记录失败");
            }

            //二级评论点赞数-1
            try {
                talkMapperExtend.increaseOrDecrementTwoComment(addLikeTwoCommentRequest.getTwoCommentId(), "2");
            } catch (Exception e) {
                throw new Exception("给二层评论点赞数-1失败");
            }


            //一层评论作者总点赞数 -1
            try {
                userInfoMapperExtend.contrLikeQuantity(addLikeTwoCommentRequest.getTwoCommentUserId(), "2");
            } catch (Exception e) {
                throw new Exception("给二层评论作者总点赞数-1失败");
            }
        }
    }


    public List<GetHotCommentResponse> getHotCommentResponseByTalkId(String talkId, String userId) throws Exception {
        try {
            return talkMapperExtend.getHotCommentResponseByTalkId(talkId, userId);
        } catch (Exception e) {
            throw new Exception("获取热门评论失败");
        }
    }

    public List<GetHotCommentResponse> getCommentResponseByTalkIdNotInHotListOrderByDate(String talkId, List<String> hotIds, Date time, String userId) throws Exception {
        try {
            return talkMapperExtend.getCommentResponseByTalkIdNotInHotListOrderByDate(talkId, hotIds, time, userId);
        } catch (Exception e) {
            throw new Exception("获取一级评论失败");
        }
    }

    public List<GetTwoCommentResponse> getTwoCommentByOneCommentIdNotInIdOrderByDate(String oneCommentId, String notTwoId, Date time, String userId) throws Exception {
        try {
            return talkMapperExtend.getTwoCommentByOneCommentIdNotInIdOrderByDate(oneCommentId, notTwoId, time, userId);
        } catch (Exception e) {
            throw new Exception("获取二级评论失败");
        }
    }

}
